//package com.iceland.testclient.client.proxy;
//
//import com.iceland.common.util.lock.LockPool;
//import io.netty.channel.ChannelFuture;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Component;
//
//import java.util.Map;
//import java.util.concurrent.ConcurrentHashMap;
//
//@Component
//public class ProxyChannelManager {
//
//    @Autowired
//    ProxyClient proxyClient;
//
//    LockPool lockPool=new LockPool();
//
//    private Map<String,ChannelFuture> channelMap=new ConcurrentHashMap<>();
//
//    /**
//     * 获得可用的Channel，没有则会创建，线程安全
//     * @param fromAddress
//     * @return
//     */
//    public ChannelFuture getProxyChannel(String fromAddress){
//        fromAddress=fromAddress.intern();
//        //存在可用的channel直接返回
//        ChannelFuture oldChannel=null;
//        if(channelMap.containsKey(fromAddress)){
//            oldChannel = channelMap.get(fromAddress);
//            if(oldChannel.isSuccess() && oldChannel.channel().isActive()){
//                return oldChannel;
//            }
//        }
//
//        ChannelFuture channelFuture=null;
//        synchronized (lockPool.intern(fromAddress)){
//            oldChannel=channelMap.get(fromAddress);
//            //不存在channel，或者channel已不可用，则创建新的channel
//            if(oldChannel==null || oldChannel.isDone() && !oldChannel.channel().isActive()){
//
//                channelFuture = proxyClient.connectServer();
//                channelMap.put(fromAddress,channelFuture);
//            }
//
//        }
//
//        return channelFuture;
//    }
//}
